package com.example.blogsystem.controller;

import com.example.blogsystem.constants.Constant;
import com.example.blogsystem.exception.UserException;
import com.example.blogsystem.model.common.Result;
import com.example.blogsystem.model.dto.FindPasswordDto;
import com.example.blogsystem.model.dto.LoginDto;
import com.example.blogsystem.model.dto.RegisterDto;
import com.example.blogsystem.model.dto.UserInfoDto;
import com.example.blogsystem.model.vo.CaptchaVo;
import com.example.blogsystem.model.vo.LoginVo;
import com.example.blogsystem.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/user")
@Tag(name = "用户操作")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result login(@RequestBody LoginDto loginDto) {
        //校验请求中的信息
        if (!StringUtils.hasLength(loginDto.getEmail())) {
            throw new UserException("邮箱输入为空");
        }
        if(!StringUtils.hasLength(loginDto.getPassword())){
            throw new UserException("密码输入为空");
        }

        LoginVo loginVo = userService.login(loginDto);

        return Result.success(loginVo);
    }

    @PostMapping("/generateCaptcha")
    @Operation(summary = "发送验证码")
    public Result generateCaptcha(String email){
        if(!StringUtils.hasLength(email)){
            throw new UserException("请输入邮箱！");
        }
        CaptchaVo captchaVo = userService.generateCaptcha(email);
        return Result.success(captchaVo);
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result register(@RequestBody RegisterDto registerDto,
                           @RequestHeader(Constant.REGISTER_CAPTCHA) String codeKey){
        if(codeKey == null){
            throw new UserException("请先发送验证码！");
        }
        if(!StringUtils.hasLength(registerDto.getUserName()) ||
                !StringUtils.hasLength(registerDto.getPassword()) ||
                !StringUtils.hasLength(registerDto.getConfirmPassword()) ||
                !StringUtils.hasLength(registerDto.getEmail()) ||
                !StringUtils.hasLength(registerDto.getCaptcha())){
            throw new UserException("输入不完整，请检查输入信息！");
        }
        if(!registerDto.getPassword().equals(registerDto.getConfirmPassword())){
            throw new UserException("两次密码不正确");
        }
        registerDto.setCodeKey(codeKey);
        userService.register(registerDto);

        return Result.success("");
    }

    @PostMapping("/findPassword")
    @Operation(summary = "找回密码")
    public Result findPassword(@RequestBody FindPasswordDto findPasswordDto,
                               @RequestHeader(Constant.REGISTER_CAPTCHA) String codeKey){
        if(codeKey == null){
            throw new UserException("请先发送验证码！");
        }
        if(!StringUtils.hasLength(findPasswordDto.getEmail()) ||
                !StringUtils.hasLength(findPasswordDto.getPassword()) ||
                !StringUtils.hasLength(findPasswordDto.getConfirmPassword()) ||
                !StringUtils.hasLength(findPasswordDto.getCaptcha())){
            throw new UserException("输入不完整，请检查输入信息！");
        }
        if(!findPasswordDto.getPassword().equals(findPasswordDto.getConfirmPassword())){
            throw new UserException("两次密码不正确");
        }
        findPasswordDto.setCodeKey(codeKey);
        userService.findPassword(findPasswordDto);
        return Result.success("");
    }

    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody UserInfoDto userInfoDto,
                             @RequestHeader(Constant.USER_TOKEN) String userToken){
        if(userToken == null){
            throw new UserException("请先登录账号！");
        }
        if(!StringUtils.hasLength(userInfoDto.getUserName()) &&
            !StringUtils.hasLength(userInfoDto.getGithubUrl()) &&
            userInfoDto.getAvatarFile() == null){
            return Result.success("");
        }
        userInfoDto.setUserToken(userToken);
        userService.updateUser(userInfoDto);
    }

}
